﻿using System;
using System.Windows.Forms;

namespace ALDIOrganizer
{
    public partial class MainForm
    {
        private void ChangeContract()
        {
            if (lbContractHistory.SelectedIndex < 0)
            {
                return;
            }
            var id = (Int64)lbContractHistory.SelectedValue;
            if (lbStaffMembers.SelectedValue == null)
            {
                return;
            }
            var employeeID = (Int64)lbStaffMembers.SelectedValue;
            var contract = SQLiteAccess.GetContract(id);
            var hired = new Contract(contract).Hired;
            Program.newOrUpdateForm = new ContractNewOrUpdate(
                SQLiteAccess.HiredContractsCount(employeeID) -
                (hired ? 1 : 0));
            var result = Program.newOrUpdateForm.ShowDialog();
            if (result == DialogResult.Cancel)
            {
                return;
            }
            // add new
            if (result == DialogResult.Yes)
            {
                Program.contractForm = new ContractDetails(
                    contract,
                    InputAction.Create);
                if (Program.contractForm.ShowDialog() == DialogResult.OK)
                {
                    SQLiteAccess.AddNewContract(
                        employeeID,
                        Program.contractForm.contract);
                }
            }
            // update existing
            if (result == DialogResult.Retry)
            {
                Program.contractForm = new ContractDetails(
                    contract,
                    InputAction.Update);

                if (Program.contractForm.ShowDialog() == DialogResult.OK)
                {
                    SQLiteAccess.UpdateContract(
                        (Int64)id,
                        Program.contractForm.contract);
                }
            }
            // delete it
            if (result == DialogResult.Abort)
            {
                // are you sure?
                SQLiteAccess.DeleteContract(id);
            }

            Hourglass(true);
            SQLiteAccess.CalculateColumns(
                DateTime.Now,
                new[] { 0, 1 },
                employeeID);
            lbContractHistory_Reload();
            Sounds.Play(Sounds.Validated); 
            Hourglass(false);
        }

        private void AddEmployee()
        {
            Program.addempToStaffForm = new AddNewEmployee(SQLiteAccess.SpawnEmployeeRow(), SQLiteAccess.SpawnContractRow());
            if (Program.addempToStaffForm.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            Hourglass(true);
            lbEmployeesRequests.Tag = true;
            SQLiteAccess.AddNewEmployee(Program.addempToStaffForm._employee, Program.addempToStaffForm._contract);
            lbEmployeesRequests.Tag = false;                
            reload_lbStaffMembers();
            UncollapseStaffDetails();
            Sounds.Play(Sounds.Validated); 
            Hourglass(false);
        }

        private void EditEmployee()
        {
            if (lbStaffMembers.SelectedValue == null)
            {
                return;
            }
            var id = lbStaffMembers.SelectedValue;
            var employee = SQLiteAccess.GetEmployee((Int64)id);
            Program.updateEmployeeForm = new UpdateEmployee(
                employee,
                InputAction.Update);
            if (Program.updateEmployeeForm.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            SQLiteAccess.UpdateEmployee((Int64)id, Program.updateEmployeeForm.Employee, false);
            reload_lbStaffMembers();
        }
    }
}
